package fr.mywiki.view.web.struts.action.admin.account;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import fr.mywiki.business.account.AccountLite;
import fr.mywiki.model.ejbAdapter.DataController;
import fr.mywiki.view.web.struts.action.ListAction;
import fr.mywiki.view.web.struts.form.ListForm;
import fr.mywiki.view.web.struts.form.admin.account.AccountListForm;

/**
 * Manages the display of the list of accounts.
 * 
 * @author tlombard
 */
public class AccountListAction extends DispatchAction implements ListAction {
	/**
	 * The name of the request attribute which contains a list of
	 * <code>AccountLite</code> objects to display. If the attribute is null, then all
	 * the accounts are displayed.
	 */
	public static final String REQUEST_ACCOUNT_LIST = "requestAccountList";

	/** Loads the list of accounts in memory and displays the right jsp */
	public ActionForward display(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse arg3) throws RemoteException {
		AccountListForm form = (AccountListForm) actionForm;
		List list = (List) request.getAttribute(REQUEST_ACCOUNT_LIST);
		
		if(list==null)
			list = new ArrayList(DataController.findAll(AccountLite.TYPE_ENT));
		
		form.init(list);

		return mapping.findForward("display");
	}

	public ActionForward selectElement(ActionMapping mapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) {
		ListForm form = (ListForm) actionForm;
		request.setAttribute(ELEMENT_ID, new Long(form.getIdElement()));
		return mapping.findForward("editAccount");
	}
}
